var productdata;
var productpage;
var curhashpath;
function sort(){
    var sort=$("#J_sorts li.cur");
    if(sort.attr("ordertype")=='3'){
        if(sort.attr("orderby")=="0")sort.attr("orderby","1");//降序
        else sort.attr("orderby","0");//顺序
    }
    query(1);
}
var hotsLoading = false;
function loadProductsNew(){
    if (hotsLoading) {
        return false;
    } else {
        hotsLoading = true;
    }

    var parm=getQueryParm();
    $w.httpRequest({
        url: apiroot + "/we/product/WeLoadproducts",
        data: {
            "classcode": parm.clscode,
            "attritems": parm.attritems,
            "pno": IPageNo,
            "ordertype": parm.ordertype,
            "orderby": parm.orderby,
            "instock": parm.instock,
            "querylike": parm.querylike,
            "floorid": parm.floorid,
            "flooritemid": parm.flooritemid
        },
        ok: function (ret) {
            hotsLoading = false;
            productdata = ret.Data.products;
            productpage = ret.Data.pagecount;
            if(productdata.length == 0){
                IPageNo = 0;
            }else {
                for (var i = 0; i < productdata.length; i++) {
                    if (productdata[i].b_chanjia && productdata[i].b_chanjia.length > 11) productdata[i].b_chanjia = productdata[i].b_chanjia.substr(0, 11) + "...";
                }
                IPageNo++;
                showProductsNew();
            }

        }
    })
}

function loadProducts(){
    var parm=getQueryParm();
    $w.httpRequest({
        url: apiroot + "/we/product/WeLoadproducts",
        data: {
            "classcode": parm.clscode,
            "attritems": parm.attritems,
            "pno": $("[name='pageno']").val(),
            "ordertype": parm.ordertype,
            "orderby": parm.orderby,
            "instock": parm.instock,
            "querylike": parm.querylike,
            "floorid": parm.floorid,
            "flooritemid": parm.flooritemid
        },
        ok: function (ret) {

            productdata = ret.Data.products;
            for (var i = 0; i < productdata.length; i++) {
                if (productdata[i].b_chanjia && productdata[i].b_chanjia.length > 11) productdata[i].b_chanjia = productdata[i].b_chanjia.substr(0, 11) + "...";
            }
            showProducts(showtype);
        }
    })
}

function selCls(obj){
    $(obj).parent().children().removeClass("cur");
    $(obj).addClass("cur");
    $("[name='clscode']").val($(obj).attr("code"));
    query(1);
}

function selattr(obj){
    $(obj).parent().children().removeClass("cur");
    $(obj).addClass("cur");
    query(1);
}

function query(n){
    var parm=getQueryParm();
    var url=ctx+"/we/product/WeList?clscode="+parm.clscode+"&attritems="+parm.attritems+"&ordertype="+parm.ordertype+"&orderby="+parm.orderby+"&instock="+parm.instock+"&floorid="+parm.floorid+"&flooritemid="+parm.flooritemid+"&pno="+n+"&querylike="+encodeURIComponent(parm.querylike)+"&showtype="+$("#J_cuts").attr("type");
    if(curhashpath)url+="#!"+curhashpath;
    window.location.href=url;
}
function getQueryParm(){
    var parm=new Object();
    var attritems="";
    $(".attrs").each(function(){
        if($("a.cur",$(this)).length>0){
            if(attritems!="")attritems+=",";
            attritems+=$("a.cur",$(this)).attr("itemid");
        }
    })
    parm.clscode=$("[name='clscode']").val();
    parm.attritems=attritems;
    var sort=$("#J_sorts li.cur");
    parm.ordertype=sort.attr("ordertype");
    parm.orderby=sort.attr("orderby");
    parm.instock=0;
    parm.querylike=$("[name='querylike']").val();
    parm.floorid=$("[name='floorid']").val();
    parm.flooritemid=$("[name='flooritemid']").val();
    return parm;
}

function showProductsNew(){
    var h='';
    h=getProductHtml_list();
    $("#productspanel").append(h);
}

function replaceHtmlFileserver(html){

    if(html){

        var sourceHtml = html;
        var fsIndex = -1;
        var targetHtml = "";
        do{
            fsIndex = sourceHtml.indexOf(fileserver);
            if( fsIndex >= 0 ){
                if( fsIndex != 0 ){
                    targetHtml += sourceHtml.substr( 0 , fsIndex );
                }
                targetHtml += getRandomFileserver();
                if( ( fsIndex + fileserver.length ) < ( sourceHtml.length - 1 ) ){
                    sourceHtml = sourceHtml.substring( fsIndex + fileserver.length );
                }
            } else {
                targetHtml += sourceHtml;
            }
        } while ( fsIndex >= 0 );

        return targetHtml;

    }
    return html;
}

//列表
function getProductHtml_list(){
    html=template('productHtml_list', {ctx:ctx,fileserver:fileserver,isuserauth:isuserauth,islogin:islogin,list:productdata});
    html = replaceHtmlFileserver(html);
    return html;
}


function addamount(obj){
    var txt=$("[name='amount']",$(obj).parent());
    var n=parseInt(txt.val());
    n++;
    txt.val(n);
    inputAmount(txt);
}
function lessenamount(obj){
    var txt=$("[name='amount']",$(obj).parent());
    var n=parseInt(txt.val());
    n--;
    txt.val(n);
    inputAmount(txt);
}
function addtocart(pid,obj){
    if(obj.className=='can'){
        var url=ctx+"/order/addToCart?pid="+pid+"&amount="+$("[name='amount']",$("[pid='"+pid+"']")).val();
        window.open(url);
    }
}

function addOften(pid,obj){
    $w.httpRequest({
        url:apiroot+"/order/addOften",
        data:{"priceid":pid},
        ok:function(ret){
            $(obj).text("已加入");
        }
    })
}

function gopage(n){
    query(n);
}
function topageclick(){
    if($("[name='topagenum']").val()!="")
        query($("[name='topagenum']").val());
}

function loadMaylikeok(datas){

    var panel=$("#maylike");
    for(var i=0;i<datas.list.length;i++){
        var data=datas.list[i];
        var h='<li class="Related-item">'
            +'<div class="Related-pic"><a href="'+ctx+'/product/info?priceid='+data.id+'" target="_blank"><img src="'+fileserver+data.photo+'" width="130" height="130" onerror="this.src=\''+ctx+'/website/images/noimg.jpg\'"/></a></div>'
            +'<div class="Related-text">';
        if(isuserauth && islogin)h+='<p><strong>￥'+data.price+'</strong></p>';
        else if(!islogin)h+='<p><font class="red">登录可见</font></p>';
        else h+='<p><font class="red">审核通过可见</font></p>';
        h+='<p><a href="'+ctx+'/product/info?priceid='+data.id+'" target="_blank">'+data.productname+' '+data.name+' '+(data.b_guige || '')+'</a></p>'
            +'</div>'
            +'</li>';
        panel.append(h);
    }
}



